<template>
    <div>
        <h1>通知类型管理</h1>
        <el-button type="text" @click="dialogFormVisible = true">添加</el-button>
        <!-- 添加弹窗 -->
        <el-dialog title="新增" :visible.sync="dialogFormVisible">
            <el-form :model="form" :rules="rules" ref="form">

                <el-form-item label="通知名称" :label-width="formLabelWidth" prop="levelName">
                    <el-input v-model="form.typeName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
                    <el-input v-model="form.sort" autocomplete="off"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addRole">确 定</el-button><!--dialogFormVisible = false-->
            </div>
        </el-dialog>


        <!-- 列表 -->
        <el-table :data="tableData" style="width: 100%" :default-sort = "{prop: 'date', order: 'descending'}">
            <el-table-column
                    prop="id"
                    label="ID"
                    width="180">
            </el-table-column>

            <el-table-column
                    prop="levelName"
                    label="通知名称"
                    width="180">

                <template slot-scope="{row}">
                    <el-form v-if="row.edit">
                        <el-form-item>
                            <el-input v-model="row.typeName" size="small"/>
                        </el-form-item>
                    </el-form>
                    <span v-else>{{ row.typeName }}</span>
                </template>

            </el-table-column>

            <el-table-column
                    prop="sort"
                    label="排序"
                    sortable
                    width="180">
                <template slot-scope="{row}">
                    <el-form v-if="row.edit">
                        <el-form-item>
                            <el-input v-model="row.sort" size="small"/>
                        </el-form-item>
                    </el-form>
                    <span v-else>{{ row.sort }}</span>
                </template>
            </el-table-column>

            <!--<el-table-column
                    prop="info"
                    label="说明"
                    width="180">
                <template slot-scope="{row}">
                    <el-form v-if="row.edit">
                        <el-form-item>
                            <el-input v-model="row.info" size="small"/>
                        </el-form-item>
                    </el-form>
                    <span v-else>{{row.info}}</span>
                </template>
            </el-table-column>-->

<!--            <el-form-item label="录入人" :label-width="formLabelWidth" prop="inputId">-->
<!--                <el-input v-model="form.inputId" autocomplete="off"></el-input>-->
<!--            </el-form-item>-->
            <el-table-column prop="teacher.name"label="录入人"  width="180">

            </el-table-column>

            <el-table-column
                    prop="inputTime"
                    label="录入时间"
                    sortable
                    width="180">
                <template slot-scope="scope">
                    <span>{{scope.row.inputTime.toLocaleString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')}}</span>
                </template>
            </el-table-column>

            <el-table-column align="center" label="操作" width="200" :resizable="false">
                <template slot-scope="{row}">

                    <el-tooltip v-if="row.edit" content="保存" placement="top">
                        <el-button
                                type="success"
                                size="small"
                                icon="el-icon-check"
                                @click="checkEditSaveOk(row)"
                        />
                    </el-tooltip>

                    <el-tooltip v-else content="编辑" placement="top">
                        <el-button
                                type="primary"
                                size="small"
                                icon="el-icon-edit"
                                @click="row.edit=!row.edit"
                        />
                    </el-tooltip>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>


    </div>
</template>

<script>
    export default {
        name: "NoticeTypeView",
        data() {
            return {
                tableData: [],
                dialogTableVisible: false,
                dialogFormVisible: false,
                form: {
                    levelName: '',
                    sort: '0',
                    // info:'',
                    inputId:'1'

                },
                formLabelWidth: '100px',
                rules: {
                    typeName: [
                        {required: true, message: '请输入等级名称', trigger: 'blur'}
                    ]
                },
            }
        },
        created() {
            this.handleInitRoles();
        },
        methods: {
            checkEditSaveOk(row) {
                // console.log(row);
                this.$axios.post("/manage/noticeType/update",row).then(response=>{
                    row.edit = !row.edit
                });
                /*row.edit = !row.edit*/
            },
            handleInitRoles() {
                this.$axios.get("/manage/noticeType/list").then(response => {
                    console.log(response)
                    const tableData = response.data.data;
                    for (var i in tableData) {
                        tableData[i]['edit']= false;
                    }
                    this.tableData = tableData;
                });
            },
            handleDelete(row){
                this.$confirm('此操作将删除该通知, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    console.log(row.id);
                    this.$axios.get("/manage/noticeType/delete",{params:{id: row.id}}).then(response => {
                        console.log(response);
                        if(response.data.code == 200){
                            /*this.tableData.splice(row,1);*/
                            this.handleInitRoles();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                        else{
                            this.$message({
                                showClose: true,
                                message: '删除出错啦',
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addRole() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        /*alert('submit!');*/
                        this.$axios.post("/manage/noticeType/save",this.form).then(response=>{
                            if(response.data.code == 200){
                                this.handleInitRoles();
                                this.dialogFormVisible = false;
                            }
                        });
                        /*this.dialogFormVisible = false;*/
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

        }
    }
</script>

<style scoped>
    .el-table{
        margin: 0 auto;
    }
</style>